﻿using BCCommon;
using BCCommon.Enums;
using BCData.Sim.StaffNotification;
using BCDto.Sim.StaffNotification;
using BCEntity.Sim.StaffNotification;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCDto.Sim.Admin.Admin;

namespace BCService.Sim.StaffNotification
{
    public class StaffNotificationService : IStaffNotificationService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IStaffNotificationData staffNotificationData;
        private readonly string key = string.Format("{0}", EntityNameConst.StaffNotificationEntity);

        public StaffNotificationService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IStaffNotificationData staffNotificationData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.staffNotificationData = staffNotificationData;
        }

        #region 检查员工通知信息是否存在
        public bool Exists(long staffNotificationId)
        {
            return this.staffNotificationData.Exists(staffNotificationId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加员工通知信息
        public StaffNotificationDto Add(StaffNotificationRequestDto requestDto)
        {
            var entity = requestDto.As<StaffNotificationEntity>();
            return this.staffNotificationData.Add(entity).GetAwaiter().GetResult().As<StaffNotificationDto>();
        }
        #endregion

        #region 修改员工通知信息
        public StaffNotificationDto Update(long staffNotificationId)
        {
            if (!this.Exists(staffNotificationId))
            {
                throw new ArgumentException("此记录不存在!");
            } 
            var entity = this.Get(staffNotificationId).As<StaffNotificationEntity>();

            var result = this.staffNotificationData.Update(entity).GetAwaiter().GetResult().As<StaffNotificationDto>();
            string cacheKey = this.redisService.GetKey(key, entity.StaffNotificationId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 删除员工通知信息
        public bool Delete(long staffNotificationId)
        {
            if (!this.Exists(staffNotificationId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.staffNotificationData.Delete(staffNotificationId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, staffNotificationId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取员工通知信息
        public StaffNotificationDto Get(long staffNotificationId)
        {
            string cacheKey = this.redisService.GetKey(key, staffNotificationId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.staffNotificationData.GetEntity(staffNotificationId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(10));
            return result.As<StaffNotificationDto>();
        }
        #endregion

        #region 获取员工通知信息分页
        public PageableList<StaffNotificationDto> Query(AdminDto adminDto, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            long adminId = adminDto.AdminId;
            string cacheKey = this.redisService.GetKey(key, adminId, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.staffNotificationData.Query(adminId,start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<StaffNotificationDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<StaffNotificationDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(10));

            return result;
        }
        #endregion


        #region 获取接收人未读消息数量
        public int GetNoneReadCount(AdminDto adminDto)
        {
            return this.staffNotificationData.GetNoneReadCount(adminDto.AdminId).Result;
        }
        #endregion
    }
}
